import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import remove from '@dfeidao/atom-mobile/local/remove';
import set from '@dfeidao/atom-mobile/local/set';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from "@dfeidao/atom-mobile/msg/service";
import toast from '@dfeidao/atom-mobile/msgbox/toast';
import { Alert } from 'react-native';
import { spaceid } from '../atom/config';

import a001 from './a001';




export default async function a003(fd: IFeidaoAiMobile) {
	// 保存
	const productid = get(fd, 'productid');
	const d = get(fd, 'data') as { stu: { subject_name: string, student_name: string, grade: string, class_name: string, section: number, date: number, student_no: string }, tea: { teacher_name: string } };
	const review_content = get(fd, 'review_content');
	if (review_content === '' || !review_content) {
		Alert.alert('课后点评内容不能为空');
		return;
	}
	const teacher_no = get(fd, 'teacher_no');
	const student_no = get(fd, 'student_no');
	const teacher_name = d.tea.teacher_name;
	const date = d.stu.date;
	const section = d.stu.section;
	const subject_name = d.stu.subject_name;
	const grade = d.stu.grade;
	const class_name = d.stu.class_name;
	const student_name = d.stu.student_name;

	const obj = { teacher_no, student_no, teacher_name, date, section, subject_name, grade, class_name, student_name, review_content };

	try {

		if (get(fd, 'prevent')) {
			return;
		}
		set(fd, 'prevent', 1);

		const res = await nodejs<{ code: number, msg: string }>('app-review-content/s002', { obj, productid });
		remove(fd, 'prevent');
		if (res.code === 1) {
			await toast({
				type: 'success',
				text: res.msg,
				position: 'top'
			});
			// 微新推送
			const arr = await nodejs<{ data1: { parent_name: string, student_name: string }, data2: { information_template_content: string }, data3: { openid: string } }>('app-review-content/s004', { filter: { student_no }, productid });

			let information_template_content = arr.data2.information_template_content;
			information_template_content = information_template_content.replace("@", arr.data1.parent_name);
			information_template_content = information_template_content.replace("@", arr.data1.student_name);

			const t = await service("thirdpart.weixinpublicno", {
				"serviceflag": "getaccesstoken",      // 固定参数
				"spaceid": spaceid,           // 当前项目的spaceid
				"thirdpay_type": "weixin_pubno", // 固定参数（weixin_pubno）
				"productid": productid    // 当前登录用户对应的productid（从系统参数params中获取）
			}) as { access_token: string };
			try {
				// console.log(d.access_token);
				const result = await service('thirdpart.weixinpubno.bulkpushmessage', {
					"tousers": [
						arr.data3.openid/// arr中的openid
						// 'oPbKtjqxTwPs_ZzDYd-1xhsYUK0s'
					],
					"spaceid": spaceid,
					"productid": productid,
					"access_token": t.access_token, /// 获取的token
					"custombody": {
						"msgtype": 'text', /// 推送的消息
						'text': {
							'content': information_template_content
						}
					}
				}) as { code: number, msg: string };
				if (result.code === 1) {
					// a001(fd);
					await toast({
						type: 'success',
						text: result.msg,
						position: 'top'
					});
					// return show_toast(message + "sssss", 'none');
					// 跳转回学生点评
					fd.page.props.navigation.navigate('app_review_student');
				}
			} catch (error) {
				await toast({
					type: 'danger',
					text: '推送失败',
					position: 'top'
				});
				await a001(fd);
				// return show_toast(message, 'none');
			}
		}
	} catch (err) {
		Alert.alert(err.message);
	}

}
